﻿using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace DynamicMVVM.Test
{
    [TestClass]
    public class DynamicModelUnitTest
    {
        private dynamic _model;

        private TestPerson _testPerson;

        [TestInitialize]
        public void Setup()
        {
            _testPerson = new TestPerson();

            _model = new DynamicModel(_testPerson);
        }

        [TestMethod]
        public void TestGetModelProperties()
        {
            _model.FirstName = "Dynamic";
            _model.LastName ="Test";
            _model.DateOfBirth = new DateTime(2001, 1, 1);

            Assert.AreEqual(_testPerson.FirstName, _model.FirstName,"Dynamic and real model should have same value");
            Assert.AreEqual(_testPerson.LastName, _model.LastName, "Dynamic and real model should have same value");
            Assert.AreEqual(_testPerson.DateOfBirth, _model.DateOfBirth, "Dynamic and real model should have same value");
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidDataException))]
        public void TestSetModelPropertyWithWrongValue()
        {
            _testPerson.DateOfBirth = new DateTime(2001, 1, 1);
            _model.DateOfBirth = "Wrong Value";
            Assert.AreNotEqual("Wrong Value", _testPerson.DateOfBirth, "Dynamic Model should not set value to real model.");
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidDataException))]
        public void TestSetInvalidDataModelValidations()
        {
            _model.FirstName = "this property has more than 25 string length value";
            
            _testPerson.LastName = "Some original value";

            _model.LastName = string.Empty;
            Assert.AreEqual(string.Empty, _testPerson.FirstName, "real model should have empty value");
            Assert.AreEqual("Some original value", _testPerson.LastName, "real model shouldn't have empty value");
        }

        [TestMethod]
        public void TestSetInvalidDataModelValidationsWithErrors()
        {
            try
            {
                _testPerson.FirstName = string.Empty;
                _model.FirstName = "this property has more than 25 string length value";
            }
            catch // shollow error
            {}
            try
            {
                _testPerson.LastName = "Some original value";
                _model.LastName = string.Empty;
            }
            catch
            {
            }

            Assert.AreEqual(string.Empty, _testPerson.FirstName, "real model should have empty value");
            Assert.AreEqual("Some original value", _testPerson.LastName, "real model shouldn't have empty value");

            Assert.IsTrue(_model.ModelState["FirstName"].Errors.Count > 0, "FirstName should have some errors");
            Assert.IsTrue(_model.ModelState["LastName"].Errors.Count > 0, "LastName should have some errors");
        }


    }
}
